package com.glory.hikvisionmobile.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.glory.hikvisionmobile.R;
import com.glory.hikvisionmobile.base.BaseActivity;
import com.glory.hikvisionmobile.base.Constant;
import com.glory.hikvisionmobile.bean.from.MatListBean;
import com.glory.hikvisionmobile.bean.from.MaterielRecordBean;
import com.glory.hikvisionmobile.bean.net.BaseResponseBean;
import com.glory.hikvisionmobile.utils.DialogUtils;
import com.glory.hikvisionmobile.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DateUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class From_MaterielRecordActivity extends BaseActivity {

    @BindView(R.id.tvTime)
    TextView tvTime;
    @BindView(R.id.rbAdd)
    RadioButton rbAdd;
    @BindView(R.id.rbRemove)
    RadioButton rbRemove;
    @BindView(R.id.etMateriel)
    FreeUI_ClearEditText etMateriel;
    @BindView(R.id.etNumber)
    FreeUI_ClearEditText etNumber;
    @BindView(R.id.etRemarks)
    FreeUI_ClearEditText etRemarks;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvReplay)
    TextView tvReplay;
    @BindView(R.id.tvDrugDetails)
    TextView tvDrugDetails;
    @BindView(R.id.tvltQTY)
    TextView tvltQTY;
    private boolean isFirst = false;
    private List<String> drugList = new ArrayList<>();
    private MaterielRecordBean materielRecordBean;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_from_materiel_record;
    }

    @Override
    protected void initView() {
        Bundle bundle = getIntent().getExtras();
        materielRecordBean = (MaterielRecordBean) bundle.getSerializable("MaterielRecordBean");
        if (materielRecordBean != null){
            tvReplay.setVisibility(View.GONE);
            rbAdd.setChecked(materielRecordBean.getWorkType().equals("L"));
            rbRemove.setChecked(materielRecordBean.getWorkType().equals("T"));
            rbAdd.setEnabled(false);
            rbRemove.setEnabled(false);
            etMateriel.setText(materielRecordBean.getMaterialName());
            etMateriel.setEnabled(false);
            etNumber.setText(materielRecordBean.getMainQty());
            etNumber.setEnabled(false);
            etRemarks.setText(materielRecordBean.getComment());
            etRemarks.setEnabled(false);
            tvTime.setText(materielRecordBean.getCreated());
            tvltQTY.setText(materielRecordBean.getLtQty());
            tvDrugDetails.setText(materielRecordBean.getMaterialDesc());
        }else {
            try {
                tvTime.setText(FreeApi_DateUtils.longToString(System.currentTimeMillis(),"yyyy-MM-dd HH:mm:ss"));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            initEdit(etMateriel, s -> {
                requestDrugDesc(s);
            },false);
        }

    }

    @Override
    protected void initData() {}

    private void requestDrugDesc(String DrugName) {
        JsonObject map = new JsonObject();
        map.addProperty("name", DrugName);
        map.addProperty("status", "Active");
        NetUtils.requestDara("HIK.PdaMatList", this, map, new FreeUI_DialogEntityCallBack<BaseResponseBean<MatListBean>>
                (new TypeToken<BaseResponseBean<MatListBean>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<MatListBean>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    if (response.body().getBody().getXPdaMats().size() <= 0){
                        showToast("数据错误，请重新录入",false);
                        return;
                    }
                    tvDrugDetails.setText(response.body().getBody().getXPdaMats().get(0).getDescription());
                    tvltQTY.setText(response.body().getBody().getXPdaMats().get(0).getLtQty());
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<MatListBean>> response) {
                super.onError(response);
                loadError(response.getException(), "HIK.PdaMatList");
            }
        });
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("物料领用退还记录表");
        freeUI_titleView.setRightListener("取消", v -> {

            if (materielRecordBean != null) {
                return;
            }

            initPermission("Refresh", this, new ActionControlListener() {
                @Override
                public void onControlChangeListener() {
                    rbAdd.setChecked(true);
                    rbRemove.setChecked(false);
                    etMateriel.setText("");
                    etNumber.setText("");
                    etRemarks.setText("");
                    isFirst = false;
                    drugList.clear();
                    tvDrugDetails.setText("");
                    tvReplay.setText("提交");
                }
            });


        });
        return new FreeUI_AddViewEntity("header", llMain);
    }



    @OnClick({R.id.tvReplay})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvReplay:

                initPermission("Submit", this, new ActionControlListener() {
                    @Override
                    public void onControlChangeListener() {
                        submit();
                    }
                });

                break;
        }
    }

    private void submit() {
        if (!editIsEmpty(etMateriel,"物料名称不能为空")){
            return;
        }

        if (!editIsEmpty(etNumber,"数量不能为空")){
            return;
        }


        if (isFirst){
            DialogUtils.showLoginDialog(this, userID -> {
                JsonObject map = new JsonObject();
                MaterielRecordBean bean = new MaterielRecordBean(rbAdd.isChecked() ? "L" : "T",etMateriel.getText().toString().trim(),tvDrugDetails.getText().toString(),
                        etNumber.getText().toString().trim(),etRemarks.getText().toString().trim(),userID);
                map.addProperty("actionType","save");
                map.addProperty("formType","WLLT");
                map.add("xPdaFormMatLingTui",new JsonParser().parse(new Gson().toJson(bean)).getAsJsonObject());
                NetUtils.requestDara("HIK.PdaForm", this, map, new FreeUI_DialogEntityCallBack<BaseResponseBean<?>>
                        (new TypeToken<BaseResponseBean<?>>() {
                        }.getType(), getSupportFragmentManager(), this) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<?>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                            showConfirmDialog(From_MaterielRecordActivity.this,"提交成功");
                            isFirst = false;

                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<?>> response) {
                        super.onError(response);
                        loadError(response.getException(), "HIK.PdaForm");
                    }
                });
            },getSupportFragmentManager());
        }else {
            DialogUtils.showLoginDialog(this, Constant.CURRENT_USER, () -> {
                showConfirmDialog("操作人确认成功");
                tvReplay.setText("确认人确认");
                isFirst = true;
            },getSupportFragmentManager());
        }

    }
}
